package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import entity.Cclass;
import entity.Teacher;
import util.DBUtil;

public class CclassDao {

	public List<Cclass> findAll(String Cclass1) {
		Connection con = DBUtil.getConnection();
		String selectAllSqlStr = "SELECT Cclass,name,id FROM students Where Cclass='" + Cclass1 + "'";
		if (con != null) {
			try {
				ResultSet rs = con.createStatement().executeQuery(selectAllSqlStr);
				List<Cclass> CclassList = rsToCclass(rs);
				System.out.println(Cclass1);
				return CclassList;
			} catch (SQLException e) {
				processSqlError(e);
			} finally {
				DBUtil.closeConnection(con);
			}
		}
		return null;
	}

	private void processSqlError(SQLException e) {
		e.printStackTrace();
		
	}

	private List<Cclass> rsToCclass(ResultSet rs) {
		List<Cclass> CclassList = new ArrayList<>();
		try {
			while (rs.next()) {
				String Cclass = rs.getString(1);
				String id = rs.getString(2);
				String name = rs.getString(3);
				Cclass cclass = new Cclass(Cclass, name,id);
				CclassList.add(cclass);
			}
			return CclassList;
		} catch (SQLException e) {
			processSqlError(e);
		}
		return null;
	}

}
